mnist10_labels = {
                0: '非洲象',
                1: '羊驼',
                2: '美洲野牛',
                3: '食蚁兽',
                4: '北极狐',
                5: '犰狳',
                6: '狒狒',
                7: '獾',
                8: '蓝鲸',
                9: '棕熊',
                10: '骆驼',
                11: '海豚',
                12: '长颈鹿',
                13: '土拨鼠',
                14: '高地牛',
                15: '马',
                16: '豺狼',
                17: '袋鼠',
                18: '考拉',
                19: '海牛',
                20: '獴',
                21: '山羊',
                22: '麋鹿',
                23: '猩猩',
                24: '水獭',
                25: '北极熊',
                26: '豪猪',
                27: '红熊猫',
                28: '犀牛',
                29: '海狮',
                30: '海豹',
                31: '雪豹',
                32: '松鼠',
                33: '糖袋鼠',
                34: '貘',
                35: '吸血蝙蝠',
                36: '小羊驼',
                37: '海象',
                38: '疣猪',
                39: '水牛',
                40: '黄鼠狼',
                41: '角马',
                42: '袋熊',
                43: '牦牛',
                44: '斑马',
            }

import os
import random
import shutil


# 重新划分为训练集和验证集
def split_dataset(source_dir, target_dir, train_ratio=0.8):
    classes = os.listdir(source_dir)
    for cls in classes:
        cls_path = os.path.join(source_dir, cls)
        if not os.path.isdir(cls_path):
            continue
        images = os.listdir(cls_path)
        random.shuffle(images)

        n_train = int(len(images) * train_ratio)
        train_imgs = images[:n_train]
        val_imgs = images[n_train:]

        for img in train_imgs:
            train_cls_path = os.path.join(target_dir, 'train', cls)
            os.makedirs(train_cls_path, exist_ok=True)
            shutil.copy(os.path.join(cls_path, img), os.path.join(train_cls_path, img))

        for img in val_imgs:
            val_cls_path = os.path.join(target_dir, 'val', cls)
            os.makedirs(val_cls_path, exist_ok=True)
            shutil.copy(os.path.join(cls_path, img), os.path.join(val_cls_path, img))

# 使用方式
split_dataset('../项目六/mammals', 'data')
